﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using iShoppingDTO;
using iShoppingDAO;

namespace iShoppingBUS
{
    public class SanPhamBUS
    {
        public static List<SanPhamDTO> getTopBySoLuotDatGia(int top)
        {

            DataTable dt = SanPhamDAO.getTopBySoLuotDatGia(top);
            List<SanPhamDTO> dssp = new List<SanPhamDTO>();
            int i, n = dt.Rows.Count;
            for (i = 0; i < n; i++)
            {
                SanPhamDTO sp = new SanPhamDTO();
                sp.MaSanPham = dt.Rows[i]["MaSanPham"].ToString();
                sp.TenSanPham = dt.Rows[i]["TenSanPham"].ToString();
                sp.MaLoaiSanPham = dt.Rows[i]["MaLoaiSanPham"].ToString();
                sp.MaTaiKhoan = dt.Rows[i]["MaTaiKhoan"].ToString();
                sp.GiaKhoiDiem = int.Parse(dt.Rows[i]["GiaKhoiDiem"].ToString());
                sp.GiaHienTai = int.Parse(dt.Rows[i]["GiaHienTai"].ToString());
                sp.NgayDang = DateTime.Parse(dt.Rows[i]["NgayDang"].ToString());
                sp.NgayHetHan = DateTime.Parse(dt.Rows[i]["NgayHetHan"].ToString());
                sp.MaTinhTrangSP = int.Parse(dt.Rows[i]["MaTinhTrangSP"].ToString());
                sp.HinhAnh = dt.Rows[i]["HinhAnh"].ToString();
                sp.SoLuotDatGia = int.Parse(dt.Rows[i]["SoLuotDatGia"].ToString());
                dssp.Add(sp);
            }
            System.Console.WriteLine(dssp.Count);
            return dssp;
        }

        public static List<SanPhamDTO> getTopByDate(int top)
        {

            DataTable dt = SanPhamDAO.getTopByDate(top);
            List<SanPhamDTO> dssp = new List<SanPhamDTO>();
            int i, n = dt.Rows.Count;
            for (i = 0; i < n; i++)
            {
                SanPhamDTO sp = new SanPhamDTO();
                sp.MaSanPham = dt.Rows[i]["MaSanPham"].ToString();
                sp.TenSanPham = dt.Rows[i]["TenSanPham"].ToString();
                sp.MaLoaiSanPham = dt.Rows[i]["MaLoaiSanPham"].ToString();
                sp.MaTaiKhoan = dt.Rows[i]["MaTaiKhoan"].ToString();
                sp.GiaKhoiDiem = int.Parse(dt.Rows[i]["GiaKhoiDiem"].ToString());
                sp.GiaHienTai = int.Parse(dt.Rows[i]["GiaHienTai"].ToString());
                sp.NgayDang = DateTime.Parse(dt.Rows[i]["NgayDang"].ToString());
                sp.NgayHetHan = DateTime.Parse(dt.Rows[i]["NgayHetHan"].ToString());
                sp.MaTinhTrangSP = int.Parse(dt.Rows[i]["MaTinhTrangSP"].ToString());
                sp.HinhAnh = dt.Rows[i]["HinhAnh"].ToString();
                sp.SoLuotDatGia = int.Parse(dt.Rows[i]["SoLuotDatGia"].ToString());
                dssp.Add(sp);
            }
            System.Console.WriteLine(dssp.Count);
            return dssp;
        }

        public static SanPhamDTO getSanPhamOfMa(String maSanPham)
        {
            return SanPhamDAO.getSanPhamOfMa(maSanPham);
        }
        public static DataTable getSanPhamOfMaSP(String maSanPham)
        {
            return SanPhamDAO.getSanPhamOfMaSP(maSanPham);
        }
        public static DataTable getSanPhamOfMaSanPhams(String maSanPham)
        {
            return SanPhamDAO.getSanPhamOfMaSanPham(maSanPham);
        }

        public static DataTable getToSanPhamOfLoai(string maloaiSP)
        {
            return SanPhamDAO.getToSanPhamOfLoai(maloaiSP);
        }
        public DataTable getTopSanPhams(DateTime ngayHetHan)
        {
            SanPhamDAO sp = new SanPhamDAO();
            return sp.getTopSanPhams(ngayHetHan);
        }
        public DataTable getVideo(string maSanPham)
        {
            SanPhamDAO sp = new SanPhamDAO();
            return sp.getVideo(maSanPham);
        }
        public static  string GetVideo(string maSanPham)
        {
            return SanPhamDAO.GetVideo(maSanPham);
        }
        public static List<SanPhamDTO> getToSanPhamOfLoaiSP(string maloaiSP)
        {

            SanPhamDAO sp = new SanPhamDAO();
            return sp.getToSanPhamOfLoaiSP(maloaiSP);
        }
        public static List<SanPhamDTO> getToSanPhamOfTenSP(string tenSanPham)
        {
            SanPhamDAO sp = new SanPhamDAO();
            return sp.getToSanPhamOfTenSP(tenSanPham);
        }

        public static SanPhamDTO LayThongTinTheoMaSanPham(string masanpham)
        {
            SanPhamDAO spDAO = new SanPhamDAO();
            DataTable dt = spDAO.LayThongTinTheoMaSanPham(masanpham);
            if (dt.Rows.Count == 0)
                return null;
            SanPhamDTO spDTO = new SanPhamDTO();
            DataRow dr = dt.Rows[0];
            spDTO.MaSanPham = dr["MaSanPham"].ToString();
            spDTO.MaTaiKhoan = dr["MaTaiKhoan"].ToString();
            spDTO.GiaHienTai = int.Parse(dr["GiaHienTai"].ToString());
            spDTO.GiaKhoiDiem = int.Parse(dr["GiaKhoiDiem"].ToString());
            spDTO.MaLoaiSanPham = dr["MaLoaiSanPham"].ToString();
            spDTO.MaTinhTrangSP = int.Parse(dr["MaTinhTrangSP"].ToString());
            spDTO.MoTaSanPham = dr["MoTaSanPham"].ToString();
            spDTO.NgayDang = DateTime.Parse(dr["NgayDang"].ToString());
            spDTO.NgayHetHan = DateTime.Parse(dr["NgayHetHan"].ToString());
            spDTO.TenSanPham = dr["TenSanPham"].ToString();
            spDTO.HinhAnh = dr["HinhAnh"].ToString();
            return spDTO;
        }

        public static bool ThemSanPham(SanPhamDTO info, List<MultimediaDTO> list_multi)
        {
            string masanpham = SanPhamDAO.ThemSanPham(info);
            List<int> list_ma_multi = MultimediaDAO.ThemMultimedia(list_multi);
            List<SanPham_MultiDTO> list_sanpham_multi = new List<SanPham_MultiDTO>();
            for (int i = 0; i < list_ma_multi.Count; i++)
            {
                SanPham_MultiDTO sp_mt = new SanPham_MultiDTO();
                sp_mt.MaMT = list_ma_multi[i];
                sp_mt.MaSanPham = masanpham;
                list_sanpham_multi.Add(sp_mt);
            }
            return SanPham_MultiDAO.AddSanPham_Multi(list_sanpham_multi);
        }

        public static bool HuyDauGia(string masanpham)
        {
            return SanPhamDAO.HuyDauGia(masanpham);
        }
         public static DataTable SelectSanPhamOfTrangThai(string maTaiKhoan,string trangThai)
         {
             return SanPhamDAO.SelectSanPhamOfTrangThai(maTaiKhoan, trangThai);
         }
         public static List<SanPhamDTO> SelectSPOfTrangThai(string maTaiKhoan, string trangThai)
         {
             return SanPhamDAO.SelectSPOfTrangThai(maTaiKhoan, trangThai);
         }

         public static DataTable SelectAll(string maLoaiSanPham)
         {
             return SanPhamDAO.SelectAll(maLoaiSanPham);
         }
        //Hue
         public static bool ThayDoiPhiBUS(string _phi)
         {
             return SanPhamDAO.ThayDoiPhiDAO(_phi);
         }
        //Hue
         public static string LayPhiHienTaiBUS()
         {
             return SanPhamDAO.LayPhiHienTaiDAO();
         }
    }
}
